package com.zsc.shixun.controller;

import com.zsc.shixun.common.CommonResult;
import com.zsc.shixun.common.ResultUtil;
import com.zsc.shixun.service.function.MyFollowingService;
import com.zsc.shixun.utils.LoginMessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 我关注的人
 * </p>
 *
 * @author ZWYZY
 * @since 2020/6/7
 */
@Controller
@RequestMapping("/following")
@Validated
public class FollowingController {
    private static final Logger logger = LoggerFactory.getLogger(FollowingController.class);
    @Autowired
    private LoginMessageUtils loginMessageUtils;
    @Autowired
    private MyFollowingService myFollowingService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        request.setAttribute("followListVO", myFollowingService.list(loginMessageUtils.getMessage().loginUser.getId()));
        return "admin/following/index";
    }

    /**
     * 获取关注列表
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @Secured(value = "ROLE_following_select")
    public CommonResult getData() {
        return ResultUtil.success(myFollowingService.list(loginMessageUtils.getMessage().loginUser.getId()));
    }

    /**
     * 取消关注
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    @Secured(value = "ROLE_following_delete")
    public CommonResult delete(@NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
        myFollowingService.delete(id, loginMessageUtils.getMessage().loginUser.getId());
        return ResultUtil.success("取消关注成功哦，亲");
    }

    /**
     * 博主页------点击关注按钮进行关注博主
     *
     * @param id
     * @return
     */
    @GetMapping("/following")
    @ResponseBody
    @Secured(value = "ROLE_following_insert")
    public CommonResult following(@NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
        myFollowingService.following(id, loginMessageUtils.getMessage().getLoginUser().getId());
        return ResultUtil.success("关注成功");
    }

    /**
     * 博主页------判断是否关注过
     * 根据是否关注显示不同的结果， res.data为true则为已关注，false则为未关注
     *
     * @param id
     * @return
     */
    @GetMapping("/isFollow")
    @ResponseBody
    @Secured(value = "ROLE_following_select")
    public CommonResult isFollow(@NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
        return ResultUtil.success(myFollowingService.isFollow(id, loginMessageUtils.getMessage().getLoginUser().getId()));
    }
}
